import { resolve } from 'path'
import { defineConfig, loadEnv } from 'vite'
import libCss from 'vite-plugin-libcss'

export default defineConfig(({mode}) => {
  const cwd = process.cwd()

  const env = loadEnv(mode, __dirname)

  const outDir = env?.['VITE_BUILD_OUT_DIR'] ?? 'dist';

  console.log('get env outDir', cwd, env, outDir)

  return {
    plugins: [
      libCss()
    ],
    server: {
      host: '0.0.0.0',
      port: 3003
    },
    build: {
      target: 'esnext',
      lib: {
        entry: resolve(__dirname, 'lib/index.ts'),
        formats: ['es']
      },
      copyPublicDir: false,
      cssCodeSplit: true,
      outDir: outDir
    },
    resolve: {
      alias: [
        {
          find: '@src',
          replacement: resolve(__dirname, './src')
        },
        {
          find: '@lib',
          replacement: resolve(__dirname, './lib')
        },
        {
          find: '~bootstrap',
          replacement: resolve(__dirname, './node_modules/bootstrap')
        }
      ]
    }
  }
})
